package org.rsbot.script.randoms;

import java.util.LinkedList;
import java.util.List;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;

@ScriptManifest(authors={"Jacmob", "HeyyamaN", "Pervy Shuya"}, name="InterfaceCloser", version=1.9D)
public class CloseAllInterface extends Random
{
  private final List<ComponentDef> components;

  public CloseAllInterface()
  {
    this.components = new LinkedList();

    addChild(743, 20);
    addChild(767, 10);
    addChild(499, 29);
    addChild(594, 48);
    addChild(275, 8);
    addChild(206, 13);
    addChild(266, 11);
    addChild(102, 13);
    addChild(14, 88, true);
    addChild(14, 11);
    addChild(157, 13);
    addChild(764, 2);
    addChild(895, 19);
    addChild(109, 14);
    addChild(667, 74);
    addChild(742, 14);
    addChild(917, 69);
  }

  private void addChild(int paramInt1, int paramInt2) {
    this.components.add(new ComponentDef(paramInt1, paramInt2, false));
  }

  private void addChild(int paramInt1, int paramInt2, boolean paramBoolean) {
    this.components.add(new ComponentDef(paramInt1, paramInt2, paramBoolean));
  }

  public boolean activateCondition()
  {
    if (this.game.isLoggedIn()) {
      if ((this.interfaces.get(755).getComponent(44).isValid()) && 
        (this.interfaces.getComponent(755, 0).getComponents().length > 0)) {
        return true;
      }

      for (ComponentDef localComponentDef : this.components) {
        RSComponent localRSComponent = this.interfaces.getComponent(localComponentDef.parent, localComponentDef.child);
        if ((localRSComponent.isValid()) && ((!localComponentDef.text) || ((localRSComponent.getText() != null) && (!localRSComponent.getText().isEmpty()))))
        {
          return true;
        }
      }
    }
    return false;
  }

  public int loop()
  {
    sleep(random(500, 900));

    if ((this.interfaces.get(755).isValid()) && (this.interfaces.getComponent(755, 0).getComponents().length > 0))
    {
      this.interfaces.getComponent(755, 44).doClick();
      return random(500, 900);
    }
    for (ComponentDef localComponentDef : this.components) {
      if (this.interfaces.getComponent(localComponentDef.parent, localComponentDef.child).isValid()) {
        this.interfaces.getComponent(localComponentDef.parent, localComponentDef.child).doClick();
        sleep(random(500, 900));
        if (random(0, 3) != 0) break;
        this.mouse.moveSlightly(); break;
      }

    }

    return -1;
  }

  static class ComponentDef
  {
    int parent;
    int child;
    boolean text;

    public ComponentDef(int paramInt1, int paramInt2, boolean paramBoolean)
    {
      this.parent = paramInt1;
      this.child = paramInt2;
      this.text = paramBoolean;
    }
  }
}